package at.jku.se.fastsell.gui;

import at.jku.se.fastsell.FastsellUI;
import at.jku.se.fastsell.data.DataAccess;
import at.jku.se.fastsell.db4o.DatabaseException;

import com.vaadin.ui.Button;
import com.vaadin.ui.Button.ClickListener;
import com.vaadin.ui.CustomComponent;
import com.vaadin.ui.FormLayout;
import com.vaadin.ui.HorizontalLayout;
import com.vaadin.ui.Label;
import com.vaadin.ui.Notification;
import com.vaadin.ui.PasswordField;
import com.vaadin.ui.TextField;
import com.vaadin.ui.Button.ClickEvent;
import com.vaadin.ui.themes.Reindeer;

/**
 * Main UI component for user registration
 */
public class RegisterComponent extends CustomComponent {

	private static final long serialVersionUID = 1L;

	private FormLayout mainLayout;
	private HorizontalLayout controlButtonLayout;
	private TextField txtUsername;
	private PasswordField passwordField, passwordFieldRetype;
	private Button btnRegister, btnCancel;
	private Label lblHead;

	// ----------------------------------------------------------------------------------

	public RegisterComponent() {
		buildMainLayout();
		setCompositionRoot(mainLayout);

	}

	// ----------------------------------------------------------------------------------

	private void buildMainLayout() {
		mainLayout = new FormLayout();
		mainLayout.setSpacing(true);
		mainLayout.setMargin(true);

		lblHead = new Label("Register");
		lblHead.setStyleName(Reindeer.LABEL_H1);
		mainLayout.addComponent(lblHead);

		txtUsername = new TextField("Username");
		txtUsername.setRequired(true);
		txtUsername.setMaxLength(12);
		mainLayout.addComponent(txtUsername);

		passwordField = new PasswordField("Password");
		passwordField.setRequired(true);
		mainLayout.addComponent(passwordField);

		passwordFieldRetype = new PasswordField("Retype password");
		passwordFieldRetype.setRequired(true);
		mainLayout.addComponent(passwordFieldRetype);

		controlButtonLayout = new HorizontalLayout();
		controlButtonLayout.setSpacing(true);
		mainLayout.addComponent(controlButtonLayout);

		btnRegister = new Button("Register");
		btnRegister.addClickListener(new ClickListener() {

			private static final long serialVersionUID = 1L;

			@Override
			public void buttonClick(ClickEvent event) {
				String username = txtUsername.getValue(), password = passwordField.getValue(), passwordRetype = passwordFieldRetype.getValue();

				if (!password.equals(passwordRetype)) {
					Notification.show("Entered passwords are not the same", Notification.Type.ERROR_MESSAGE);
				} else if (password.equals("") || username.equals("")) {
					Notification.show("Please enter a username and password", Notification.Type.ERROR_MESSAGE);
				} else if (DataAccess.checkIfUserExists(username)) {
					Notification.show("Username already exists", Notification.Type.ERROR_MESSAGE);
				} else {
					try {
						DataAccess.createUser(username, password);
						Notification.show("User '" + username + "' successfully created");
					} catch (DatabaseException e) {
						Notification.show("Could not create account: " + e.toString(), Notification.Type.ERROR_MESSAGE);
					}
				}
			}
		});
		controlButtonLayout.addComponent(btnRegister);

		btnCancel = new Button("Cancel");
		btnCancel.addClickListener(new ClickListener() {

			private static final long serialVersionUID = 1L;

			@Override
			public void buttonClick(ClickEvent event) {
				FastsellUI.navigateTo(EntryView.NAME);
			}
		});
		controlButtonLayout.addComponent(btnCancel);

	}

}
